package com.atguigu.lease.web.admin.service.impl;

import com.atguigu.lease.common.constant.RedisConstant;
import com.atguigu.lease.model.entity.*;
import com.atguigu.lease.model.enums.ItemType;
import com.atguigu.lease.model.enums.ReleaseStatus;
import com.atguigu.lease.web.admin.mapper.*;
import com.atguigu.lease.web.admin.service.*;
import com.atguigu.lease.web.admin.vo.attr.AttrValueVo;
import com.atguigu.lease.web.admin.vo.graph.GraphVo;
import com.atguigu.lease.web.admin.vo.room.RoomDetailVo;
import com.atguigu.lease.web.admin.vo.room.RoomItemVo;
import com.atguigu.lease.web.admin.vo.room.RoomQueryVo;
import com.atguigu.lease.web.admin.vo.room.RoomSubmitVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.swagger.v3.oas.annotations.media.Schema;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * @author liubo
 * @description 针对表【room_info(房间信息表)】的数据库操作Service实现
 * @createDate 2023-07-24 15:48:00
 */
@Service
public class RoomInfoServiceImpl extends ServiceImpl<RoomInfoMapper, RoomInfo>
        implements RoomInfoService {

    @Autowired
    private  RoomInfoMapper roomInfoMapper;
    @Autowired
private GraphInfoMapper   graphInfoMapper;

    @Autowired
    private AttrValueMapper   attrValueMapper;
    @Autowired
    private FacilityInfoMapper facilityInfoMapper;
    @Autowired
    private LabelInfoMapper   labelInfoMapper;

    @Autowired
    private PaymentTypeMapper  paymentTypeMapper;

    @Autowired
    private LeaseTermMapper   leaseTermMapper;
    @Autowired
    private GraphInfoService graphInfoService;
    @Autowired
   private   RoomFacilityService roomFacilityService;
    @Autowired
    private RoomLabelService roomLabelService;
    @Autowired
    private RoomAttrValueService roomAttrValueService;
    @Autowired
    private  RoomPaymentTypeService roomPaymentTypeService;
    @Autowired
    private  RoomLeaseTermService roomLeaseTermService;
    @Autowired
    private RedisTemplate<String,Object> redisTemplate;

    @Override//报错或更新
    public void saveOrUpdateRoom(RoomSubmitVo roomSubmitVo) {



        //因为id回显的原因 先判断传过来的数据id是否为空  如果有id证明是更新操作 没用则是插入操作
        boolean isUpdate = roomSubmitVo.getId() != null;
        //先插入房间基本信息
        super.saveOrUpdate(roomSubmitVo);
        //不为空时则执行删除操作 把房间的所有关系删除掉 在重新插入
        if (!isUpdate) {
            //1.删除图片列表
            LambdaQueryWrapper<GraphInfo> GraphQueryWrapper = new LambdaQueryWrapper<>();
            GraphQueryWrapper.eq(GraphInfo::getItemType,ItemType.ROOM);
            GraphQueryWrapper.eq(GraphInfo::getItemId,roomSubmitVo.getId());
           graphInfoService.remove(GraphQueryWrapper);


            //2.删除配套列表
            LambdaQueryWrapper<RoomFacility> facilityInfoLambdaQueryWrapper = new LambdaQueryWrapper<>();
             facilityInfoLambdaQueryWrapper.eq(RoomFacility::getRoomId,roomSubmitVo.getId());
                roomFacilityService.remove(facilityInfoLambdaQueryWrapper);

            //3.删除标签列表
            LambdaQueryWrapper<RoomLabel> roomLabelLambdaQueryWrapper = new LambdaQueryWrapper<>();
              roomLabelLambdaQueryWrapper.eq(RoomLabel::getRoomId,roomSubmitVo.getId());
              roomLabelService.remove(roomLabelLambdaQueryWrapper);

            //4.删除属性信息列表
            LambdaQueryWrapper<RoomAttrValue> roomAttrValueLambdaQueryWrapper = new LambdaQueryWrapper<>();
            roomAttrValueLambdaQueryWrapper.eq(RoomAttrValue::getRoomId,roomSubmitVo.getId());
            roomAttrValueService.remove(roomAttrValueLambdaQueryWrapper);

            //5.删除支付方式列表

            LambdaQueryWrapper<RoomPaymentType> roomPaymentTypeLambdaQueryWrapper = new LambdaQueryWrapper<>();
            roomPaymentTypeLambdaQueryWrapper.eq(RoomPaymentType::getRoomId,roomSubmitVo.getId());
            roomPaymentTypeService.remove(roomPaymentTypeLambdaQueryWrapper);
            //6.删除可选租期列表

            LambdaQueryWrapper<RoomLeaseTerm> roomLeaseTermLambdaQueryWrapper = new LambdaQueryWrapper<>();
            roomLeaseTermLambdaQueryWrapper.eq(RoomLeaseTerm::getRoomId,roomSubmitVo.getId());
            roomLabelService.remove(roomLabelLambdaQueryWrapper);
            //7.删除缓存
            redisTemplate.delete(RedisConstant.APP_ROOM_PREFIX + roomSubmitVo.getId());
        }
        //执行插入操作
          //前端传过来封装在vo里 我们执行插入操作映射的是实体类，所以我们要把vo转实体类

        //1.插入图片列表
        List<GraphVo> graphVoList = roomSubmitVo.getGraphVoList();
        if (!CollectionUtils.isEmpty(graphVoList)) {
            ArrayList<GraphInfo> graphInfoList = new ArrayList<>();
            for (GraphVo graphVo : graphVoList) {
                 GraphInfo graphInfo = new GraphInfo();
                   graphInfo.setItemType(ItemType.ROOM);
                   graphInfo.setItemId(roomSubmitVo.getId());
                   graphInfo.setName(graphVo.getName());
                   graphInfo.setUrl(graphVo.getUrl());
                   graphInfoList.add(graphInfo);

            }
            graphInfoService.saveBatch(graphInfoList);
        }

        //2.插入房间配套列表
        List<Long> facilityInfoIds = roomSubmitVo.getFacilityInfoIds();
        if (!CollectionUtils.isEmpty(facilityInfoIds)) {
            List<RoomFacility> roomFacilityList = new ArrayList<>();
            for (Long facilityInfoId : facilityInfoIds) {
                RoomFacility roomFacility = RoomFacility.builder().roomId(roomSubmitVo.getId()).facilityId(facilityInfoId).build();
                roomFacilityList.add(roomFacility);
            }
            roomFacilityService.saveBatch(roomFacilityList);
        }
        //3.插入标签列表
        //保存新的labelInfoList
        List<Long> labelInfoIds = roomSubmitVo.getLabelInfoIds();
        if (!CollectionUtils.isEmpty(labelInfoIds)) {
            ArrayList<RoomLabel> roomLabelList = new ArrayList<>();
            for (Long labelInfoId : labelInfoIds) {
                RoomLabel roomLabel = RoomLabel.builder().roomId(roomSubmitVo.getId()).labelId(labelInfoId).build();
                roomLabelList.add(roomLabel);
            }
            roomLabelService.saveBatch(roomLabelList);
        }
        //4.插入属性信息列表
        List<Long> attrValueIds = roomSubmitVo.getAttrValueIds();
        if (!CollectionUtils.isEmpty(attrValueIds)) {
            List<RoomAttrValue> roomAttrValueList = new ArrayList<>();
            for (Long attrValueId : attrValueIds) {
                RoomAttrValue roomAttrValue = RoomAttrValue.builder().roomId(roomSubmitVo.getId()).attrValueId(attrValueId).build();
                roomAttrValueList.add(roomAttrValue);
            }
            roomAttrValueService.saveBatch(roomAttrValueList);
        }
        //5.插入支付方式列表
        List<Long> paymentTypeIds = roomSubmitVo.getPaymentTypeIds();
        if (!CollectionUtils.isEmpty(paymentTypeIds)) {
            List<RoomPaymentType> roomPaymentTypeList = new ArrayList<>();
            for (Long paymentTypeId : paymentTypeIds) {
              RoomPaymentType roomPaymentType=   RoomPaymentType.builder().roomId(roomSubmitVo.getId()).paymentTypeId(paymentTypeId).build();
                roomPaymentTypeList.add(roomPaymentType);
            }
            roomPaymentTypeService.saveBatch(roomPaymentTypeList);
        }

        //6.插入可选租期列表
        List<Long> leaseTermIds = roomSubmitVo.getLeaseTermIds();
        if (!CollectionUtils.isEmpty(leaseTermIds)) {
            ArrayList<RoomLeaseTerm> roomLeaseTerms = new ArrayList<>();
            for (Long leaseTermId : leaseTermIds) {
                RoomLeaseTerm roomLeaseTerm = RoomLeaseTerm.builder().roomId(roomSubmitVo.getId()).leaseTermId(leaseTermId).build();
                roomLeaseTerms.add(roomLeaseTerm);
            }
            roomLeaseTermService.saveBatch(roomLeaseTerms);
        }


    }

    @Override  //根据省份 城市 地区 公寓id 分页获取房间信息
    public IPage<RoomItemVo> pageItem(Page<RoomItemVo> page, RoomQueryVo queryVo) {

        IPage<RoomItemVo> iPage= roomInfoMapper.pageItem(page,queryVo);
        return iPage;
    }

    @Override//更具房间id获取房间的全部信息
    public RoomDetailVo getDetailById(Long id) {

        //获取房间基本信息
         RoomInfo roomInfo=  roomInfoMapper.selectById(id);

        //所属公寓信息
         ApartmentInfo apartmentInfo= roomInfoMapper.selectApartmentById(id);

        //图片列表
         List<GraphVo> graphVo=    graphInfoMapper.selectByRoomId( ItemType.ROOM,id);

        //  属性信息列表
         List<AttrValueVo> attrValueVoList= attrValueMapper.selectByRoomId(id);

        //配套信息列表
          List<FacilityInfo> facilityInfoList= facilityInfoMapper.selectListByRoomId(id);

        //标签信息列表
        List<LabelInfo> labelInfoList = labelInfoMapper.selectListByRoomId(id);


       //支付方式列表
        List<PaymentType> paymentTypeList = paymentTypeMapper.selectByRoomId();

       // 可选租期列表
        List<LeaseTerm> leaseTermList = leaseTermMapper.selectByRoomId(id);


      //组合结果
        RoomDetailVo roomDetailVo =new RoomDetailVo();
        BeanUtils.copyProperties(roomInfo, roomDetailVo);
        //BeanUtils.copyProperties(apartmentInfo, roomDetailVo);
        roomDetailVo.setApartmentInfo(apartmentInfo);
        roomDetailVo.setGraphVoList(graphVo);
        roomDetailVo.setAttrValueVoList(attrValueVoList);
        roomDetailVo.setFacilityInfoList(facilityInfoList);
        roomDetailVo.setLabelInfoList(labelInfoList);
        roomDetailVo.setPaymentTypeList(paymentTypeList);
        roomDetailVo.setLeaseTermList(leaseTermList);

        return roomDetailVo;
    }

    @Override//删除房间以及相关的信息
    public void removeRoomAndJoinById(Long id) {

        //删除房间
        super.removeById(id);

        //1.删除图片列表
        LambdaQueryWrapper<GraphInfo> GraphQueryWrapper = new LambdaQueryWrapper<>();
        GraphQueryWrapper.eq(GraphInfo::getItemType,ItemType.ROOM);
        GraphQueryWrapper.eq(GraphInfo::getItemId,id);
        graphInfoService.remove(GraphQueryWrapper);


        //2.删除配套列表
        LambdaQueryWrapper<RoomFacility> facilityInfoLambdaQueryWrapper = new LambdaQueryWrapper<>();
        facilityInfoLambdaQueryWrapper.eq(RoomFacility::getRoomId,id);
        roomFacilityService.remove(facilityInfoLambdaQueryWrapper);

        //3.删除标签列表
        LambdaQueryWrapper<RoomLabel> roomLabelLambdaQueryWrapper = new LambdaQueryWrapper<>();
        roomLabelLambdaQueryWrapper.eq(RoomLabel::getRoomId,id);
        roomLabelService.remove(roomLabelLambdaQueryWrapper);

        //4.属性信息列表
        LambdaQueryWrapper<RoomAttrValue> roomAttrValueLambdaQueryWrapper = new LambdaQueryWrapper<>();
        roomAttrValueLambdaQueryWrapper.eq(RoomAttrValue::getRoomId,id);
        roomAttrValueService.remove(roomAttrValueLambdaQueryWrapper);

        //5.支付方式列表

        LambdaQueryWrapper<RoomPaymentType> roomPaymentTypeLambdaQueryWrapper = new LambdaQueryWrapper<>();
        roomPaymentTypeLambdaQueryWrapper.eq(RoomPaymentType::getRoomId,id);
        roomPaymentTypeService.remove(roomPaymentTypeLambdaQueryWrapper);
        //6.可选租期列表

        LambdaQueryWrapper<RoomLeaseTerm> roomLeaseTermLambdaQueryWrapper = new LambdaQueryWrapper<>();
        roomLeaseTermLambdaQueryWrapper.eq(RoomLeaseTerm::getRoomId,id);
        roomLabelService.remove(roomLabelLambdaQueryWrapper);

        //7.删除缓存
        redisTemplate.delete(RedisConstant.APP_ROOM_PREFIX + id);
    }

    @Override//更新房间的发布状态
    public void updateReleaseStatusById(Long id, ReleaseStatus status) {
        LambdaUpdateWrapper<RoomInfo> roomInfoLambdaUpdateWrapper = new LambdaUpdateWrapper<>();
        roomInfoLambdaUpdateWrapper.eq(RoomInfo::getId,id);
        roomInfoLambdaUpdateWrapper.set(RoomInfo::getIsRelease,status);
        super.update(roomInfoLambdaUpdateWrapper);

    }

    @Override//根据公寓id查询房间
    public List<RoomInfo> listBasicByApartmentId(Long id) {

        LambdaUpdateWrapper<RoomInfo> roomInfoLambdaUpdateWrapper = new LambdaUpdateWrapper<>();
        roomInfoLambdaUpdateWrapper.eq(RoomInfo::getApartmentId,id);
        roomInfoLambdaUpdateWrapper.set(RoomInfo::getIsRelease,ReleaseStatus.RELEASED);
        List<RoomInfo> list= super.list(roomInfoLambdaUpdateWrapper);
        return list;
    }
}




